/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TSinais.java
 *
 * Created on 03/03/2012, 15:44:31
 */
package br.ufrn.dca.gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import br.ufrn.dca.controlelrl.TanqueMalhaAberta;


/**
 *
 * @author Solano
 */
public class TelaMalhaAberta extends javax.swing.JPanel {

	private TelaPrincipal principal1;
	
	/** Creates new form TSinais */
    public TelaMalhaAberta(TelaPrincipal principal1) {
    	this.principal1 = principal1;
        initComponents();
        this.setSize(principal1.getSize());
//        this.setSize(750,700);
    }
    
    public TelaPrincipal getPrincipal1() {
		return principal1;
	}

	public void setPrincipal1(TelaPrincipal principal1) {
		this.principal1 = principal1;
	}



	public JTextField getAmpMax() {
        return ampMax;
    }

    public JTextField getAmpMin() {
        return ampMin;
    }

    public ChartPanel getGraficoPanelEscrita() {
		return graficoPanelEscrita;
	}

	public void setGraficoPanelEscrita(ChartPanel graficoPanelEscrita) {
		this.graficoPanelEscrita = graficoPanelEscrita;
	}

	public ChartPanel getGraficoPanelLeitura() {
		return graficoPanelLeitura;
	}

	public void setGraficoPanelLeitura(ChartPanel graficoPanelLeitura) {
		this.graficoPanelLeitura = graficoPanelLeitura;
	}

	/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        botaoDegrau = new javax.swing.JToggleButton();
        botaoDegrau.setToolTipText("Degrau");
        botaoDegrau.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent evt) {
        		butaoDegrauActionPerformed(evt);
        	}
        });
        botaoQuadrada = new javax.swing.JToggleButton();
        botaoQuadrada.setToolTipText("Quadrada");
        botaoSeno = new javax.swing.JToggleButton();
        botaoSeno.setToolTipText("Senoidal");
        botaoSeno.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent evt) {
        		butaoSenoActionPerformed(evt);
        	}
        });
        botaoAleatorio = new javax.swing.JToggleButton();
        botaoAleatorio.setToolTipText("Aleat\u00F3rio");
        botaoAleatorio.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent evt) {
        		butaoAleatorioActionPerformed(evt);
        	}
        });
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        ampMax = new javax.swing.JTextField();
        ampMin = new javax.swing.JTextField();
        graficoPanelEscrita = new ChartPanel((JFreeChart) null);

        botaoDegrau.setIcon(new ImageIcon(TelaMalhaAberta.class.getResource("/br/ufrn/dca/gui/iconD.jpg"))); // NOI18N

        botaoQuadrada.setIcon(new ImageIcon(TelaMalhaAberta.class.getResource("/br/ufrn/dca/gui/iconQ.jpg"))); // NOI18N
        botaoQuadrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butaoQuadradaActionPerformed(evt);
            }
        });

        botaoSeno.setIcon(new ImageIcon(TelaMalhaAberta.class.getResource("/br/ufrn/dca/gui/iconS.jpg"))); // NOI18N

        botaoAleatorio.setIcon(new ImageIcon(TelaMalhaAberta.class.getResource("/br/ufrn/dca/gui/iconAleatorio.png"))); // NOI18N

        jLabel1.setText("Amplitude MAX:");

        jLabel2.setText("Amplitude  MIN:");

        javax.swing.GroupLayout graficoPanelLayout = new javax.swing.GroupLayout(graficoPanelEscrita);
        graficoPanelLayout.setHorizontalGroup(
        	graficoPanelLayout.createParallelGroup(Alignment.LEADING)
        		.addGap(0, 645, Short.MAX_VALUE)
        );
        graficoPanelLayout.setVerticalGroup(
        	graficoPanelLayout.createParallelGroup(Alignment.LEADING)
        		.addGap(0, 208, Short.MAX_VALUE)
        );
        graficoPanelEscrita.setLayout(graficoPanelLayout);
        
        botaoSerra = new JToggleButton("");
        botaoSerra.setToolTipText("Dente de Serra");
        botaoSerra.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent evt) {
        		butaoSerraActionPerformed(evt);
        	}
        });
        botaoSerra.setIcon(new ImageIcon(TelaMalhaAberta.class.getResource("/br/ufrn/dca/gui/iconDS.jpg")));
        
        lblPerodo = new JLabel("Per\u00EDodo:");
        
        periodo = new JTextField();
        periodo.setColumns(10);
        
        lblS = new JLabel("s");
        lblS.setFont(new Font("Tahoma", Font.BOLD, 11));
        
        graficoPanelLeitura = new ChartPanel((JFreeChart) null);
        GroupLayout gl_chartPanel_1 = new GroupLayout(graficoPanelLeitura);
        gl_chartPanel_1.setHorizontalGroup(
        	gl_chartPanel_1.createParallelGroup(Alignment.LEADING)
        		.addGap(0, 631, Short.MAX_VALUE)
        		.addGap(0, 598, Short.MAX_VALUE)
        );
        gl_chartPanel_1.setVerticalGroup(
        	gl_chartPanel_1.createParallelGroup(Alignment.LEADING)
        		.addGap(0, 254, Short.MAX_VALUE)
        		.addGap(0, 276, Short.MAX_VALUE)
        );
        graficoPanelLeitura.setLayout(gl_chartPanel_1);
        
        JLabel lblTenso = new JLabel("Tens\u00E3o (V):");
        
        valorTensao = new JLabel("0.0");
        
        lblNvel = new JLabel("N\u00EDvel (cm):");
        
        valorNivel = new JLabel("0.0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addGroup(layout.createParallelGroup(Alignment.LEADING, false)
        				.addGroup(layout.createSequentialGroup()
        					.addGap(18)
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addComponent(jLabel1)
        						.addGroup(layout.createSequentialGroup()
        							.addComponent(lblPerodo)
        							.addPreferredGap(ComponentPlacement.UNRELATED)
        							.addComponent(periodo, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
        						.addComponent(jLabel2))
        					.addPreferredGap(ComponentPlacement.UNRELATED)
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
        							.addComponent(ampMin, Alignment.LEADING)
        							.addComponent(ampMax, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
        						.addComponent(lblS))
        					.addGap(18)
        					.addGroup(layout.createParallelGroup(Alignment.LEADING, false)
        						.addGroup(layout.createSequentialGroup()
        							.addComponent(botaoDegrau, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
        							.addPreferredGap(ComponentPlacement.UNRELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        							.addComponent(botaoQuadrada, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE))
        						.addGroup(layout.createSequentialGroup()
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addComponent(lblTenso)
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addComponent(valorTensao)))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addGroup(layout.createSequentialGroup()
        							.addComponent(lblNvel)
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addComponent(valorNivel))
        						.addGroup(layout.createSequentialGroup()
        							.addComponent(botaoSeno, GroupLayout.PREFERRED_SIZE, 63, GroupLayout.PREFERRED_SIZE)
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addComponent(botaoAleatorio, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addComponent(botaoSerra, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE))))
        				.addGroup(layout.createSequentialGroup()
        					.addContainerGap()
        					.addGroup(layout.createParallelGroup(Alignment.LEADING)
        						.addComponent(graficoPanelLeitura, GroupLayout.PREFERRED_SIZE, 645, GroupLayout.PREFERRED_SIZE)
        						.addComponent(graficoPanelEscrita, GroupLayout.PREFERRED_SIZE, 645, GroupLayout.PREFERRED_SIZE))))
        			.addGap(11))
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.TRAILING)
        		.addGroup(layout.createSequentialGroup()
        			.addGap(40)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblPerodo)
        				.addComponent(periodo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblS)
        				.addComponent(lblNvel)
        				.addComponent(valorNivel)
        				.addComponent(lblTenso)
        				.addComponent(valorTensao))
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addGroup(layout.createParallelGroup(Alignment.LEADING)
        				.addGroup(layout.createSequentialGroup()
        					.addGap(8)
        					.addGroup(layout.createParallelGroup(Alignment.BASELINE, false)
        						.addGroup(layout.createSequentialGroup()
        							.addGap(6)
        							.addComponent(jLabel1))
        						.addComponent(ampMax, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        					.addPreferredGap(ComponentPlacement.UNRELATED)
        					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        						.addComponent(ampMin, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        						.addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)))
        				.addComponent(botaoDegrau, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
        				.addComponent(botaoQuadrada, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
        				.addComponent(botaoSeno, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
        				.addComponent(botaoAleatorio, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
        				.addComponent(botaoSerra, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(graficoPanelEscrita, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(graficoPanelLeitura, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE)
        			.addGap(30))
        );
        this.setLayout(layout);
        
        limparGrafico();
        
    }// </editor-fold>                        

	public void limparGrafico() {
		XYSeriesCollection datasetEscrita = new XYSeriesCollection();
		XYSeriesCollection datasetLeitura = new XYSeriesCollection();
        
		escrita = new XYSeries("Escrita");
		leitura = new XYSeries("Leitura");

		datasetEscrita.addSeries(escrita);
		JFreeChart graficoEscrita = ChartFactory.createXYLineChart(
				"Escrita Tanque Quanser", // The chart title
				"", // x axis label
				"", // y axis label
				datasetEscrita, // The dataset for the chart
				PlotOrientation.VERTICAL,
				false, // Is a legend required?
				false, // Use tooltips
				false // Configure chart to generate URLs?
				);
		
		escrita.add(0, 0);
		
		datasetLeitura.addSeries(leitura);
		JFreeChart graficoLeitura = ChartFactory.createXYLineChart(
				"Leitura Tanque Quanser", // The chart title
				"", // x axis label
				"", // y axis label
				datasetLeitura, // The dataset for the chart
				PlotOrientation.VERTICAL,
				false, // Is a legend required?
				false, // Use tooltips
				false // Configure chart to generate URLs?
				);
        
        leitura.add(0, 0);
        
        graficoPanelEscrita.setChart(null);
        graficoPanelEscrita.setChart(graficoEscrita);
        
        graficoPanelLeitura.setChart(null);
        graficoPanelLeitura.setChart(graficoLeitura);
	}

    private void butaoQuadradaActionPerformed(java.awt.event.ActionEvent evt) {                                              
        if (botaoQuadrada.isSelected())
        	iniciar(TanqueMalhaAberta.RECT);
		else
			interromper();
        
        botaoAleatorio.setSelected(false);
        botaoSeno.setSelected(false);
        botaoDegrau.setSelected(false);
        botaoSerra.setSelected(false);
        ampMin.setEditable(true);
    }
    
    private void butaoSenoActionPerformed(ActionEvent evt) {
    	if (botaoSeno.isSelected())
    		iniciar(TanqueMalhaAberta.SIN);
		else
			interromper();
    	
    	botaoAleatorio.setSelected(false);
        botaoQuadrada.setSelected(false);
        botaoDegrau.setSelected(false);
        botaoSerra.setSelected(false);
        ampMin.setEditable(true);
	}
    
    private void butaoAleatorioActionPerformed(ActionEvent evt) {
    	if (botaoAleatorio.isSelected())
    		iniciar(TanqueMalhaAberta.RANDOM_SIGNAL);
		else
			interromper();
    	
        botaoQuadrada.setSelected(false);
        botaoSeno.setSelected(false);
        botaoDegrau.setSelected(false);
        botaoSerra.setSelected(false);
        ampMin.setEditable(true);
	}

	private void butaoSerraActionPerformed(ActionEvent evt) {
		if (botaoSerra.isSelected())
			iniciar(TanqueMalhaAberta.SAWTOOTH);
		else
			interromper();
		
		botaoAleatorio.setSelected(false);
        botaoQuadrada.setSelected(false);
        botaoSeno.setSelected(false);
        botaoDegrau.setSelected(false);
        ampMin.setEditable(true);
	}
	
	private void butaoDegrauActionPerformed(ActionEvent evt) {
		if (botaoDegrau.isSelected())
			iniciar(TanqueMalhaAberta.HEAVISIDE);
		else
			interromper();
		
		botaoAleatorio.setSelected(false);
        botaoQuadrada.setSelected(false);
        botaoSeno.setSelected(false);
        botaoSerra.setSelected(false);
        ampMin.setEditable(false);
	}
	
	private void iniciar(int i) {
		principal1.inicializarTanqueMalhaAberta(i);
	}
	
	private void interromper() {
		principal1.interromperTanqueEmExecucao(false);
	}
	
    public JLabel getValorTensao() {
		return valorTensao;
	}

	public void setValorTensao(JLabel valorTensao) {
		this.valorTensao = valorTensao;
	}
	
	public JLabel getValorNivel() {
		return valorNivel;
	}

	public void setValorNivel(JLabel valorNivel) {
		this.valorNivel = valorNivel;
	}

	public XYSeries getEscrita() {
		return escrita;
	}

	public void setEscrita(XYSeries escrita) {
		this.escrita = escrita;
	}

	public XYSeries getLeitura() {
		return leitura;
	}

	public void setLeitura(XYSeries leitura) {
		this.leitura = leitura;
	}
	
	public JTextField getPeriodo() {
		return periodo;
	}



	// Variables declaration - do not modify                     
    private javax.swing.JTextField ampMax;
    private javax.swing.JTextField ampMin;
    private javax.swing.JToggleButton botaoAleatorio;
    private javax.swing.JToggleButton botaoDegrau;
    private javax.swing.JToggleButton botaoQuadrada;
    private javax.swing.JToggleButton botaoSeno;
    private javax.swing.JToggleButton botaoSerra;
    private ChartPanel graficoPanelEscrita;
    private ChartPanel graficoPanelLeitura;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private JLabel lblPerodo;
    private JTextField periodo;
    private JLabel lblS;
    private XYSeries escrita;
    private XYSeries leitura;
    private JLabel valorTensao;
    private JLabel lblNvel;
    private JLabel valorNivel;
}
